<?php
require_once(LIBPATH.'sql.php');
require_once(LIBPATH.'dBug.php');

class news extends table
{
    public function News($db)
    {
        //$this->base = new PDO(BASESTRING, BASEUSER, BASEPASWD);
        parent::table('news', NULL, $db);
        $this->addColumn('id'        , 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY');
        $this->addColumn('user'      , 'varchar(10)  NOT NULL' );
        $this->addColumn('title'     , 'varchar(255) NOT NULL');
        $this->addColumn('subTitle'  , 'varchar(255)');
        $this->addColumn('flags'     , 'varchar(255)');
        $this->addColumn('regDate'   , 'date');
        $this->addColumn('regTime'   , 'time');
        $this->addColumn('news'      , 'text');
        $result = $this->db->query($this->create());
    }

	/**
	 * Make insert or update in datadb...
	 *
	 */
    public function insert($values)
    {
        str_replace('\n', '', $values['news'] );

      	if ($values['id'] == null)
		{
			unset($values['id']);
            $values['regDate']  = date('Y-m-d');
        	$values['regTime']  = date('H:i');
        	$sql = parent::insert($values);
		}
		else
		{
			$this->clear();
			$this->addWhere('id=', $values['id']);
			$sql = parent::update($values);
		}
        $result = $this->db->query($sql);
        return $result;

    }



    /**
    * List all news registered.
    *
    */
    function listAll()
    {
        $this->clear();
        $sql    = $this->select();
        $result = $this->db->query($sql);
        return $result;
    }


    function listLast($limit=null, $flags=null)
    {
        $this->clear();
        $this->setOrderBy('id desc');
        if ($limit)
        {
            $this->setLimit(0, $limit);
        }
        if ($flags)
        {
            $this->addWhere('flags=', $flags);
        }
        $sql    = $this->select();
        $result = $this->db->query($sql);
        $return = $this->resultToArray($result);
        return $return;
    }

    /**
    * Remove an User, passing it Id.
    *
    */
    function delete($id)
    {
        $this->clear();
        $this->addWhere("id  ="  , $id);
        return $this->db->query( parent::delete() );
    }

	/**
	 * 	Function to get a News.
     *  Please pass it id.
	 */
    function get($id)
    {
        $this->clear();
        $this->addWhere('id=',$id);
        $sql = $this->select();
        $result = $this->db->query($sql);
        return $result;
    }
}
